package com.techblor.wechat.library.api;

import com.techblor.wechat.library.bean.message.response.*;
import com.thoughtworks.xstream.XStream;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信消息接口
 */
@Slf4j
public class MessageApi {

    /**
     * 将接收的消息XML转换为Map
     *
     * @param request
     * @return
     */
    public Map<String, String> xmlToMap(HttpServletRequest request) {
        // 将解析结果存储在map中
        Map<String, String> map = new HashMap<>();
        InputStream inputStream = null;
        try {
            // 从request中取得输入流
            inputStream = request.getInputStream();
            SAXReader reader = new SAXReader();
            Document doc = reader.read(inputStream);
            Element root = doc.getRootElement();
            List<Element> list = root.elements();

            for (Element e : list) {
                map.put(e.getName(), e.getText());
            }
        } catch (Exception e) {
            log.error("解析xml异常: {}", e.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("关闭输入流异常: {}", e.getMessage());
                }
            }
        }

        return map;
    }

    /**
     * 将响应的文本消息对象转换成XML
     *
     * @return
     */
    public String textMessageToXml(TextMessage textMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", textMessage.getClass());
        return xstream.toXML(textMessage);

    }

    /**
     * 将响应的图片消息对象转换成xml
     *
     * @param imageMessage
     * @return
     */
    public String imageMessageToXml(ImageMessage imageMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", imageMessage.getClass());
        return xstream.toXML(imageMessage);
    }

    /**
     * 语音消息对象转换成xml
     *
     * @param voiceMessage
     * @return
     */
    public String voiceMessageToXml(VoiceMessage voiceMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", voiceMessage.getClass());
        return xstream.toXML(voiceMessage);
    }

    /**
     * 音乐消息对象转换成xml
     *
     * @param musicMessage
     * @return
     */
    public String messageToXml(MusicMessage musicMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", musicMessage.getClass());
        return xstream.toXML(musicMessage);
    }

    /**
     * 将图文消息转换为XML
     *
     * @param newsMessage
     * @return
     */
    public String newsMessageToXml(NewsMessage newsMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", newsMessage.getClass());
        xstream.alias("item", new Article().getClass());
        return xstream.toXML(newsMessage);
    }

}
